package com.zzxy.spring5.factory;
import aspect.MyAspect;
import com.zzxy.spring5.aspect.MyAspect;
import com.zzxy.spring5.service.IStudentService;
import com.zzxy.spring5.service.impl.IStudentService;
import  com.zzxy.spring5.service.impl.StudentServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyBeanFactory {



    public static IStudentService createStudentService() {
        StudentServiceImpl studentService = new StudentServiceImpl();
        MyAspect aspect = new MyAspect();
       IStudentService stuServiceProxy = (IStudentService) Proxy.newProxyInstance(MyBeanFactory.class.getClassLoader(), studentService.getClass().getInterfaces(), new InvocationHandler() {
           @Override
           public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
               aspect.before();;
               Object obj = method.invoke(studentService,args);
               aspect.after();
               return obj;
           }





        });
        return stuServiceProxy;
    }
    }

